﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Code4WP7SDK
{
    public class VmNavigateHelper
    {
        /// <summary>
        /// ViewModel中的页面跳转辅助方法
        /// </summary>
        /// <param name="url"></param>
        public static void Navigate(string url)
        {
            PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
            root.Navigate(new Uri(url, UriKind.Relative));
        }

        /// <summary>
        /// ViewModel中更严谨的跳转方法 -- 注意包含操作
        /// </summary>
        /// <param name="currentUrl"></param>
        /// <param name="navigateUrl"></param>
        public static void Navigate(string currentUrl, string navigateUrl)
        {
            PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
            if (root.CurrentSource.ToString().Contains(currentUrl))
            {
                root.Navigate(new Uri(navigateUrl, UriKind.Relative));
            }
        }
        /// <summary>
        /// 跳转回上一页面 用Contains函数显示包含了有参数的情况
        /// </summary>
        /// <param name="currentUrl"></param>
        public static void GoBack(string currentUrl)
        {
            PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
            if (root.CurrentSource.ToString().Contains(currentUrl) && root.CanGoBack)
            {
                root.GoBack();
            }
        }
    }
}
